import { defineStore } from 'pinia'
import { reactive } from 'vue'
import instance from '@/api'

interface Staff {
  id: number
  name: string
  phone: string
  address: string
  services: string[]
  workPeriods: string[]
  status: number
  rank: string
  salary: number
}

interface StaffState {
  staffList: Staff[]
  availableStaffList: Staff[],
  shopList:[]
}

export const useStaffStore = defineStore('staff', {
  state: (): StaffState => ({
    staffList: [],
    availableStaffList: [],
    shopList: []
  }),
  
  actions: {
    async fetchStaffList() {
      try {
        const resp = await instance.get('/api/staff/getStaffs')
        this.staffList = resp.data
        this.updateAvailableStaffList()
      } catch (error) {
        console.error('获取阿姨列表失败:', error)
      }
    },
    async fetchShopList(){
      const resp = await instance.get('/api/shop/getList')
      this.shopList = resp.data
    },
    updateAvailableStaffList() {
      this.availableStaffList = this.staffList.filter((staff: Staff) => staff.status === 1)
    },

    updateStaffStatus(id: number, status: number) {
      const staff = this.staffList.find((s: Staff) => s.id === id)
      if (staff) {
        staff.status = status
        this.updateAvailableStaffList()
      }
    }
  }
}) 